package com.midea.ai.air.utils;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.midea.ai.air.R;

/**
 * Created by ouyangdi on 2016/4/7.
 * 定义一些公共的方法
 */
public class ComUtils {

    public static boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = { 0, 0 };
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    public static void createDialog(Context context, String title, String content){
        View view= LayoutInflater.from(context).inflate(R.layout.dialog_item, null);
        final Dialog dialog = new Dialog(context, R.style.my_activate_dialog);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(view);

        TextView dialogTitle = (TextView) dialog.findViewById(R.id.dialog_item_textView_title);
        dialogTitle.setText(title);
        TextView dialogContent = (TextView) dialog.findViewById(R.id.dialog_item_textView_msg);
        dialogContent.setText(content);
        TextView dialog_cancel = (TextView) dialog.findViewById(R.id.dialog_item_textView_cancel);
        TextView dialog_confirm = (TextView) dialog.findViewById(R.id.dialog_item_textView_confirm);
        dialog_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
}
